package ylh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ylh.common.R;
import ylh.entity.Category;
import ylh.service.CategoryService;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize) {
        log.info("page = {},pageSize = {}", page, pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();

        //按更新时间排序
        queryWrapper.orderByAsc(Category::getSort);

        //执行查询
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    @GetMapping("/{id}")
    public R<Category> getById(@PathVariable Long id) {
        log.info("根据id查询菜品信息");
        Category category = categoryService.getById(id);
        if (category != null) return R.success(category);
        return R.error("没有查到对应菜品信息");
    }

    @DeleteMapping
    public R<String> delete(Long ids) {
        log.info("根据id删除分类");
        categoryService.remove(ids);
        return R.success("删除成功");
    }


    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("新增分类，分类信息：{}", category.toString());
//        LocalDateTime dateTime = LocalDateTime.now();
//        category.setCreateTime(dateTime);
//        category.setUpdateTime(dateTime);
//
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        category.setCreateUser(empId);
//        category.setUpdateUser(empId);
        if (categoryService.save(category)) {
            return R.success("添加成功");
        } else return R.error("添加失败");
    }


    @PutMapping
    public R<String> update(@RequestBody Category category, HttpServletRequest request) {
        log.info("修改分类信息：{}",category);
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        category.setUpdateUser(empId);
//        category.setUpdateTime(LocalDateTime.now());
        categoryService.updateById(category);
        return R.success("更改成功");
    }
    
    @GetMapping("/list")
    public R<List<Category>> getByType(Category category){
        
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        
        List<Category> list = categoryService.list(queryWrapper );
        
        if (list != null) return R.success(list);
        return R.error("没有查到该分类");
    }
}
